/**
 * 
 */
package com.example.downloadmanagerdemo.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.downloadmanagerdemo.R;
import com.example.downloadmanagerdemo.download.DownloadError;
import com.example.downloadmanagerdemo.download.DownloadInfo;
import com.example.downloadmanagerdemo.download.DownloadManager;
import com.example.downloadmanagerdemo.download.DownloadStatus;
import com.example.downloadmanagerdemo.download.DownloadStatusCallback;

/**
 * @author careyjwang
 * 
 */
public class DownloadAdapter extends BaseAdapter implements DownloadStatusCallback {
	private List<DownloadInfo> mDownloadInfos;
	private LayoutInflater mInflater;
	private DownloadManager mDownloadManager;

	public DownloadAdapter(List<DownloadInfo> infos, Context context, DownloadManager manager) {
		mDownloadInfos = infos;
		mInflater = LayoutInflater.from(context);
		mDownloadManager = manager;
		mDownloadManager.registerDownloadCallback(this);
	}
	
	@Override
	public int getCount() {
		return mDownloadInfos.size();
	}

	@Override
	public Object getItem(int position) {
		return mDownloadInfos.get(position);
	}

	@Override
	public long getItemId(int position) {
		return mDownloadInfos.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		DownloadInfo info = mDownloadInfos.get(position);
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.download_item, parent, false);
			holder = new ViewHolder();
			holder.progress = (TextView) convertView.findViewById(R.id.download_progress);
			holder.downloadBtn = (Button) convertView.findViewById(R.id.download_btn);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.downloadBtn.setTag(info);
		holder.downloadBtn.setOnClickListener(mDownloadClickListener);
		switch (info.getDownloadState()) {
		case DownloadStatus.STATUS_INIT: {
			holder.downloadBtn.setText("下载");
		}
			break;
		case DownloadStatus.STATUS_DOWNLOADING: {
			holder.progress.setText("下载中" + info.getProgress());
			holder.downloadBtn.setText("暂停");
		}
			break;
		case DownloadStatus.STATUS_PAUSE: {
			holder.progress.setText("暂停中" + info.getProgress());
			holder.downloadBtn.setText("继续");
		}
			break;
		case DownloadStatus.STATUS_WAIT: {
			holder.progress.setText("等待");
		}
			break;
		case DownloadStatus.STATUS_FAILED: {
			holder.progress.setText("失败");
			holder.downloadBtn.setText("下载");
		}
			break;
		case DownloadStatus.STATUS_SUCCESS: {
			holder.progress.setText("恭喜你下载完成，快去安装试玩吧^_^");
			holder.downloadBtn.setText("安装");
		}
			break;
		}
		return convertView;
	}

	private OnClickListener mDownloadClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			DownloadInfo info = (DownloadInfo) v.getTag();
			switch (info.getDownloadState()) {
			case DownloadStatus.STATUS_INIT:
			case DownloadStatus.STATUS_FAILED: {
				mDownloadManager.startDownload(info);
			}
				break;
			case DownloadStatus.STATUS_SUCCESS: {
				mDownloadManager.install(info);
			}
				break;
			case DownloadStatus.STATUS_DOWNLOADING: {
				mDownloadManager.pauseDownload(info);
			}
				break;
			case DownloadStatus.STATUS_PAUSE: {
				mDownloadManager.resumeDownload(info);
			}
				break;
			default:
				break;
			}

		}
	};

	private static class ViewHolder {
		TextView progress;
		Button downloadBtn;
	}

	@Override
	public void onDownloadStatusChanged(int status, String packageName) {
		for (DownloadInfo info : mDownloadInfos) {
			if (info.getPackageName().equals(packageName)) {
				info.setDownloadState(status);
			}
		}
		notifyDataSetChanged();
	}

	@Override
	public void onDownloadProgressChanged(int progress, String packageName) {
		for (DownloadInfo info : mDownloadInfos) {
			if (info.getPackageName().equals(packageName)) {
				info.setProgress(progress);
			}
		}
		notifyDataSetChanged();
	}

	@Override
	public void onDownloadError(DownloadError error) {
		for (DownloadInfo info : mDownloadInfos) {
			if (info.getPackageName().equals(error.packageName)) {
				info.setDownloadState(DownloadStatus.STATUS_FAILED);
			}
		}
		notifyDataSetChanged();
	}
}
